package com.bonc.rms.service.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bonc.rms.dao.QueryDao;
import com.bonc.rms.entity.SeniorQueryEntity;
import com.bonc.rms.entity.TestEntity;
import com.bonc.rms.jpa.UserJpa;
import com.bonc.rms.service.QueryService;

@Service(value = "queryServiceImpl")
public class QueryServiceImpl implements QueryService {
	@Autowired
	QueryDao queryDao;
	@Autowired
	UserJpa userJpa;

	@Override
	public List<Map<String, Object>> detailQueryResume(String queryValue, int pageNo) {
		String[] queryValueArray = queryValue.split(" ");
		List<String> queryList = new LinkedList<>();
		for (int i = 0; i < queryValueArray.length; i++) {
			if (!queryValueArray[i].equals(" ")) {
				queryList.add(queryValueArray[i]);
			}
		}
		return queryDao.detailQuery(queryList, pageNo);
	}

	@Override
	public List<Map<String, Object>> seniorQueryResume(SeniorQueryEntity seniorQueryEntity, int pageNo) {
		Map<String, Object> dataMap = new HashMap<>();
		if (seniorQueryEntity.getWorkingExperience() != null && !seniorQueryEntity.getWorkingExperience().isEmpty()) {
			dataMap.put("workingExperience", seniorQueryEntity.getWorkingExperience());
		}
		if (seniorQueryEntity.getWorkExperience() != null && !seniorQueryEntity.getWorkExperience().isEmpty()) {
			dataMap.put("workExperience", seniorQueryEntity.getWorkExperience());
		}
		if ((seniorQueryEntity.getCareerObjective() != null) && !seniorQueryEntity.getCareerObjective().isEmpty()) {
			dataMap.put("careerObjective", seniorQueryEntity.getCareerObjective());
		}
		if ((seniorQueryEntity.getDegree() != null) && !seniorQueryEntity.getDegree().isEmpty()) {
			dataMap.put("degree", seniorQueryEntity.getDegree());
		}
		if ((seniorQueryEntity.getEducation() != null) && !seniorQueryEntity.getEducation().isEmpty()) {
			dataMap.put("education", seniorQueryEntity.getEducation());
		}
		if (seniorQueryEntity.getGender() != null && !seniorQueryEntity.getGender().isEmpty()) {
			dataMap.put("gender", seniorQueryEntity.getGender());
		}
		if (seniorQueryEntity.getCompany() != null && !seniorQueryEntity.getCompany().isEmpty()) {
			dataMap.put("company", seniorQueryEntity.getCompany());
		}
		if (dataMap.entrySet().size() == 0) {
			return queryDao.seniorQuery("queryType", "queryValue", pageNo);
		} else {
			return queryDao.seniorQuery(dataMap, pageNo);
		}
	}

	@Override
	public void saveShowSelect(TestEntity testEntity) {
		userJpa.save(testEntity);
	}

	@Override
	public String queryShowSelect(int i) {
		return userJpa.findById(i);
	}

}
